package com.fat.controller;

import com.fat.pojo.Category;
import com.fat.pojo.Result;
import com.fat.service.CategoryService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 文章分类控制器
 */
@RestController
@RequestMapping("/category")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 添加分类
     */
    @PostMapping
    public Result addCategory(@RequestBody @Validated(Category.Add.class) Category category){
        return categoryService.addCategory(category);
    }

    /**
     * 获取分类列表
     *      获取的是当前登录用户创建的分类
     */
    @GetMapping
    public Result listCategory(){
        return categoryService.listCategory();
    }

    /**
     * 获取文章分类详情
     */
    @GetMapping("/detail")
    public Result getCategoryDetail(@RequestParam("id") @NotNull int id){
        return categoryService.getCategoryDetail(id);
    }

    /**
     * 更新文章分类
     */
    @PutMapping
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category){
        return categoryService.updateCategory(category);
    }

    /**
     * 删除文章分类
     */
    @DeleteMapping
    public Result deleteCategory(@RequestParam("id") @NotNull int id){
        return categoryService.deleteCategory(id);
    }


}
